package org.budo.phone.location.service.symphony;

import java.util.List;

import org.budo.phone.location.service.api.BudoPhoneLocation;
import org.budo.phone.location.service.api.BudoPhoneLocationService;
import org.budo.support.lang.util.ListUtil;
import org.budo.support.lang.util.StringUtil;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Getter
@Setter
@Slf4j
public class SymphonyPhoneLocationService implements BudoPhoneLocationService {
    private List<BudoPhoneLocationService> phoneLocationServices;

    public BudoPhoneLocation seekPhoneLocation(String phone) {
        if (ListUtil.isNullOrEmpty(this.getPhoneLocationServices()) || StringUtil.isNullOrEmpty(phone)) {
            log.warn("#24 return null, phoneLocationService is null, phoneLocationServices=" + this.getPhoneLocationServices() + ", phone=" + phone + ", this=" + this);
            return null;
        }

        for (BudoPhoneLocationService phoneLocationService : this.getPhoneLocationServices()) {
            BudoPhoneLocation phoneLocation = this.seekPhoneLocation(phoneLocationService, phone);
            if (null != phoneLocation) {
                return phoneLocation;
            }
        }

        log.info("#35 return null, phoneLocationServices=" + this.getPhoneLocationServices() + ", this=" + this);
        return null;
    }

    private BudoPhoneLocation seekPhoneLocation(BudoPhoneLocationService budoPhoneLocationService, String phone) {
        try {
            return budoPhoneLocationService.seekPhoneLocation(phone);
        } catch (Throwable e) {
            log.error("#43 seekPhoneLocation error, e=" + e, e);
            return null;
        }
    }
}